//index.js
import storage from '../../mpf/cache/storage.js';
import tokenManager from '../../mpf/auth/token-manager.js';
Component({
  properties: {
    // 1:普通标题栏  2:首页tab标题栏  3:搜索标题栏  4:带签到的搜索标题栏
    type: {
      type: Number,
      observer: function (newData, oldData) {
        this.getHeight()
      }
    },
    title:{
      type: String
    },
    search:{
      type: Boolean,
      value: false
    }
  },
  data: {
    titleBarHeight: 0,
    routeLength: 0,
    dotPosition: null,
    vivoHeight: 0
  },
  methods: {
    getHeight: function(){
      // 获取路由配置
      var route = getCurrentPages();
      this.setData({
        routeLength: route.length
      })
      // 判断是否已经获得标题栏高度
      if (storage.get('hHeight') != '' && storage.get('hHeight')){
        this.setData({
          titleBarHeight: storage.get('hHeight').titleBarHeight,
          vivoHeight: storage.get('hHeight').vivoHeight,
          dotPosition: storage.get('hHeight').dotPosition
        })
        return
      }
      // 兼容vivo设备增加高度
      var vivoHeight = 0;
      wx.getSystemInfo({
        success: (res) => {
          if (res.model.indexOf('vivo') != -1) {
            vivoHeight = res.statusBarHeight - 5;
            this.setData({
              vivoHeight: res.statusBarHeight
            })
          }
        }
      })
      // 获取标题栏胶囊位置和大小信息
      var dotPosition = wx.getMenuButtonBoundingClientRect();
      this.setData({
        titleBarHeight: dotPosition.top + dotPosition.height + vivoHeight + 10,
        dotPosition: dotPosition
      })
      // 保存vivo手机高度和tab标题栏高度
      var hHeight ={
        vivoHeight: this.data.vivoHeight,
        titleBarHeight: this.data.titleBarHeight,
        dotPosition: this.data.dotPosition,
        statusBarHeight: 0
      }
      // 存储当前高度
      storage.set('hHeight', hHeight);
    },
    goBack: function(){
      if (this.properties.search){
        // 关闭时发射当前选择的属性
        this.triggerEvent('headerevent')
        return
      }
      wx.navigateBack({
        delate: 1
      })
    },
    goHome: function(){
      wx.switchTab({
        url: '/pages/index/index',
      })
    },
    goSearch: function(){
      wx.navigateTo({
        url: '/pages/search/index',
      })
    },
    goSign: function(){
      if(!tokenManager.get()){
        global.login();
        return
      }
      wx.navigateTo({
        url: '/pages/sign/index',
      })
    }
  }
})

